package com.fbty.mt.dwr;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.directwebremoting.WebContextFactory;

import com.fbty.base.domain.Page;
import com.fbty.base.dwr.DwrAuthHelper;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.ManagerService;

public class ManagerDwr {
	private ManagerService managerService;
	/**
	 * 分页显示共享类ip
	 * @param pageNo
	 * @param flag
	 * @param parms
	 * @return
	 */
	public Page pageIpInsos(Integer pageNo,boolean flag,String parms) {
		return managerService.pageIpInsos(pageNo, Page.PRODUCT_PAGE_SIZE, parms);
	}
	/**
	 * 放入购物车
	 * @param pids
	 * @return
	 */
	public Integer buy(String pids) {
		HttpSession session = WebContextFactory.get().getSession();
		HashSet obj = (HashSet) session.getAttribute("shoppingcar");
		String[] arr = pids.split(":");
		for(String val : arr) {
			if(!"".equals(val)) {
				obj.add(val);
			}
		}
		session.setAttribute("shoppingcar",obj);
		return obj.size();
	}
	/**
	 * 查看共享类购物车
	 * @param pids
	 * @return
	 */
	public Page selectBuy(Integer pageNo) {
		HttpSession session = WebContextFactory.get().getSession();
		HashSet set = (HashSet) session.getAttribute("shoppingcar");
		return managerService.selectBuy(set,pageNo,Page.PRODUCT_PAGE_SIZE);
	}
	/**
	 * 购买共享类IP
	 * @param pids
	 * @return
	 */
	public String saveBuy() {
		HttpSession session = WebContextFactory.get().getSession();
		HashSet set = (HashSet) session.getAttribute("shoppingcar");
		User user = (User) session.getAttribute("user");
		Integer num = (Integer) session.getAttribute("num");
		String message = managerService.updateBuy(set,user,num,session);
		Set<String> obj = new HashSet<String>();
		session.setAttribute("shoppingcar",obj);
		return message;
	}
	/**
	 * 显示价格
	 * @param pids
	 * @return
	 */
	public List showPrice() {
		String roleId =  (String) WebContextFactory.get().getSession().getAttribute("roleId");
		return managerService.showPrice(roleId);
	}
	
	/**
	 * 显示用户信息
	 * @param pids
	 * @return
	 */
	public User showUser() {
		return (User) WebContextFactory.get().getSession().getAttribute("user");
	}
	/**
	 *更新用户信息
	 * @param pids
	 * @return
	 */
	public void updateUser(Integer id,String name,String username,String phone,String email,String qq,String pwd){
		User user = (User) WebContextFactory.get().getSession().getAttribute("user");
		if(!"".equals(pwd)) {
			user.setPassword(pwd);
		}
		user.setEmail(email);
		user.setUsername(username);
		user.setQq(qq);
		managerService.updateUser(user);
	}
	/**
	 * 批量删除购物车内的记录
	 * @param ids
	 */
	public Integer delForSelectBuy(String ids) {
		String[] arr = ids.split(":");
		HashSet set = (HashSet) DwrAuthHelper.getSession().getAttribute("shoppingcar");
		for(String id : arr) {
			set.remove(id);
		}
		return set.size();
	}
	/**
	 * 管理员设定价格
	 * @param pids
	 * @return
	 */
	public List<Map<String, String>> price(String params) {
		return managerService.price(params);
	}
	/**
	 * 管理员修改价格
	 * @param pids
	 * @return
	 */
	public void updatePrice(Integer id,String num,Double price,Integer time) {
		managerService.updatePrice(id, num, price,time);
	}
	/**
	 * 管理员删除价格
	 * @param pids
	 * @return
	 */
	public void deletePriceById(Integer id) {
		managerService.deletePriceById(id);
	}
	/**
	 * 新增价格
	 * @param pids
	 * @return
	 */
	public void addPrice(Integer num,Double price,Integer type) {
		managerService.addPrice(num, price, type);
	}

	/**
	 * 用户退出
	 * @return
	 */
	public void loginOut() {
		WebContextFactory.get().getSession().removeAttribute("user");
	}
	public ManagerService getManagerService() {
		return managerService;
	}
	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}

}
